Switch from a custom MTime to the filetime crate
authorAlex Crichton <alex@alexcrichton.com>
Fri, 29 May 2015 20:49:13 +0000 (13:49 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 3 Jun 2015 01:05:47 +0000 (18:05 -0700)
The crate provides the same functionality necessary for Cargo, but it's
maintained elsewhere now.

Cargo.lock
Cargo.toml
src/cargo/lib.rs
src/cargo/ops/cargo_rustc/fingerprint.rs
src/cargo/sources/path.rs
src/cargo/util/mod.rs
src/cargo/util/mtime.rs [deleted file]

index 7639e26d1a22c5a2e2f016dd7be141991a90b1ad..2136c33bcfb01f2c4eee97af3e2923ae06f78883 100644 (file)
@@ -7,6 +7,7 @@ dependencies = [
  "curl 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
  "docopt 0.6.64 (registry+https://github.com/rust-lang/crates.io-index)",
  "env_logger 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "filetime 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "flate2 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "git2 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
  "git2-curl 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -91,6 +92,11 @@ dependencies = [
  "regex 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "filetime"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "flate2"
 version = "0.2.7"
index c9323fb8eaa2a59bed24985c5a208ffc6584c9f0..f4340c43b3c4c0a9a381c6f13dcb2b4420afebce 100644 (file)
@@ -31,6 +31,7 @@ threadpool = "0.1"
 time = "0.1"
 toml = "0.1"
 url = "0.2"
+filetime = "0.1"
 
 [target.i686-pc-windows-gnu]
 dependencies = { winapi = "0.1", advapi32-sys = "0.1", kernel32-sys = "0.1" }
index e9c98355b5e3103af90bef19e8e55878fb2d4a97..e5c57245757d11157ddf4b4c344ff29a0c05f92d 100644 (file)
@@ -1,11 +1,12 @@
 #![deny(unused)]
-#![feature(metadata_ext, file_type, dir_entry_ext)]
+#![feature(file_type, dir_entry_ext)]
 #![cfg_attr(test, deny(warnings))]
 
 #[cfg(test)] extern crate hamcrest;
 #[macro_use] extern crate log;
 extern crate curl;
 extern crate docopt;
+extern crate filetime;
 extern crate flate2;
 extern crate git2;
 extern crate glob;
index 8ca4800f26149a8d60b9fc1234679516446b5212..05e5ddd1af310abecef39d3c378dde0994a4975a 100644 (file)
@@ -4,8 +4,10 @@ use std::io::{BufReader, SeekFrom};
 use std::path::{Path, PathBuf};
 use std::sync::{Arc, Mutex};
 
+use filetime::FileTime;
+
 use core::{Package, Target, Profile};
-use util::{self, MTime};
+use util;
 use util::{CargoResult, Fresh, Dirty, Freshness, internal, profile, ChainError};
 
 use super::Kind;
@@ -99,7 +101,7 @@ struct FingerprintInner {
 #[derive(Clone)]
 enum LocalFingerprint {
     Precalculated(String),
-    MtimeBased(Option<MTime>, PathBuf),
+    MtimeBased(Option<FileTime>, PathBuf),
 }
 
 impl FingerprintInner {
@@ -118,7 +120,8 @@ impl FingerprintInner {
             LocalFingerprint::MtimeBased(Some(n), _) if !force => n.to_string(),
             LocalFingerprint::MtimeBased(_, ref p) => {
                 debug!("resolving: {}", p.display());
-                try!(MTime::of(p)).to_string()
+                let meta = try!(fs::metadata(p));
+                FileTime::from_last_modification_time(&meta).to_string()
             }
         };
         let resolved = util::short_hash(&(&known, &self.extra, &deps));
@@ -326,7 +329,7 @@ fn is_fresh(loc: &Path, new_fingerprint: &Fingerprint) -> CargoResult<bool> {
     Ok(old_fingerprint == new_fingerprint)
 }
 
-fn calculate_target_mtime(dep_info: &Path) -> CargoResult<Option<MTime>> {
+fn calculate_target_mtime(dep_info: &Path) -> CargoResult<Option<FileTime>> {
     macro_rules! fs_try {
         ($e:expr) => (match $e { Ok(e) => e, Err(..) => return Ok(None) })
     }
@@ -339,7 +342,8 @@ fn calculate_target_mtime(dep_info: &Path) -> CargoResult<Option<MTime>> {
         Some(Ok(line)) => line,
         _ => return Ok(None),
     };
-    let mtime = try!(MTime::of(dep_info));
+    let meta = try!(fs::metadata(&dep_info));
+    let mtime = FileTime::from_last_modification_time(&meta);
     let pos = try!(line.find(": ").chain_error(|| {
         internal(format!("dep-info not in an understood format: {}",
                          dep_info.display()))
@@ -357,13 +361,14 @@ fn calculate_target_mtime(dep_info: &Path) -> CargoResult<Option<MTime>> {
             file.push(' ');
             file.push_str(deps.next().unwrap())
         }
-        match MTime::of(&cwd.join(&file)) {
-            Ok(file_mtime) if file_mtime <= mtime => {}
-            Ok(file_mtime) => {
-                info!("stale: {} -- {} vs {}", file, file_mtime, mtime);
-                return Ok(None)
-            }
-            _ => { info!("stale: {} -- missing", file); return Ok(None) }
+        let meta = match fs::metadata(cwd.join(&file)) {
+            Ok(meta) => meta,
+            Err(..) => { info!("stale: {} -- missing", file); return Ok(None) }
+        };
+        let file_mtime = FileTime::from_last_modification_time(&meta);
+        if file_mtime > mtime {
+            info!("stale: {} -- {} vs {}", file, file_mtime, mtime);
+            return Ok(None)
         }
     }
 
index d1f6f97fade023dcbb029e5b71ec8abd6ac8552b..a3f058573dcd505c985ce891355f151222fceb73 100644 (file)
@@ -4,13 +4,14 @@ use std::fs;
 use std::io::prelude::*;
 use std::path::{Path, PathBuf};
 
+use filetime::FileTime;
 use git2;
 use glob::Pattern;
 
 use core::{Package, PackageId, Summary, SourceId, Source, Dependency, Registry};
 use ops;
 use util::{self, CargoResult, internal, internal_error, human, ChainError};
-use util::{MTime, Config};
+use util::Config;
 
 pub struct PathSource<'cfg> {
     id: SourceId,
@@ -308,14 +309,16 @@ impl<'cfg> Source for PathSource<'cfg> {
             return Err(internal_error("BUG: source was not updated", ""));
         }
 
-        let mut max = MTime::zero();
+        let mut max = FileTime::zero();
         for file in try!(self.list_files(pkg)).iter() {
             // An fs::stat error here is either because path is a
             // broken symlink, a permissions error, or a race
             // condition where this path was rm'ed - either way,
             // we can ignore the error and treat the path's mtime
             // as 0.
-            let mtime = MTime::of(&file).unwrap_or(MTime::zero());
+            let mtime = fs::metadata(file).map(|meta| {
+                FileTime::from_last_modification_time(&meta)
+            }).unwrap_or(FileTime::zero());
             warn!("{} {}", mtime, file.display());
             max = cmp::max(max, mtime);
         }
index e29fa5844541d6f0cbb047f00419ab8d476ad72b..095884d776d4bcbeff1d25e48d540f8916479e7f 100644 (file)
@@ -15,7 +15,6 @@ pub use self::to_url::ToUrl;
 pub use self::to_semver::ToSemver;
 pub use self::vcs::{GitRepo, HgRepo};
 pub use self::sha256::Sha256;
-pub use self::mtime::MTime;
 
 pub mod config;
 pub mod errors;
@@ -32,4 +31,3 @@ pub mod lev_distance;
 mod dependency_queue;
 mod sha256;
 mod vcs;
-mod mtime;
diff --git a/src/cargo/util/mtime.rs b/src/cargo/util/mtime.rs
deleted file mode 100644 (file)
index 13d4296..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-use std::fmt;
-use std::fs;
-use std::io;
-use std::path::Path;
-
-/// A helper structure to represent the modification time of a file.
-///
-/// The actual value contained within is platform-specific and does not have the
-/// same meaning across platforms, but comparisons and stringification can be
-/// significant among platforms.
-#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Copy, Clone)]
-pub struct MTime {
-    seconds: u64,
-    nanos: u32,
-}
-
-impl MTime {
-    pub fn zero() -> MTime {
-        MTime { seconds: 0, nanos: 0 }
-    }
-
-    pub fn of(p: &Path) -> io::Result<MTime> {
-        let metadata = try!(fs::metadata(p));
-        Ok(MTime::from(&metadata))
-    }
-}
-
-impl<'a> From<&'a fs::Metadata> for MTime {
-    #[cfg(unix)]
-    fn from(meta: &'a fs::Metadata) -> MTime {
-        use std::os::unix::prelude::*;
-        let raw = meta.as_raw();
-        // FIXME: currently there is a bug in the standard library where the
-        //        nanosecond accessor is just accessing the seconds again, once
-        //        that bug is fixed this should take nanoseconds into account.
-        MTime { seconds: raw.mtime() as u64, nanos: 0 }
-    }
-
-    #[cfg(windows)]
-    fn from(meta: &'a fs::Metadata) -> MTime {
-        use std::os::windows::prelude::*;
-
-        // Windows write times are in 100ns intervals, so do a little math to
-        // get it into the right representation.
-        let time = meta.last_write_time();
-        MTime {
-            seconds: time / (1_000_000_000 / 100),
-            nanos: ((time % (1_000_000_000 / 100)) * 100) as u32,
-        }
-    }
-}
-
-impl fmt::Display for MTime {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "{}.{:09}s", self.seconds, self.nanos)
-    }
-}